<?php

class commonAction extends Action{

    public function _initialize(){
            if (!isset($_SESSION['uid']) || !isset($_SESSION['username'])) {
                    redirect(U('Login/index'));
            }
    }

    
	//Uploadify品牌上传处理
	Public function uploadBrand () {
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize = 2000000 ;	//最大上传大小
		$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');//充许上传文件的类型
		$upload->uploadReplace = true;	//覆盖同名文件
		$upload->saveRule = 'uniqid';
		$upload->savePath = './Uploads/Brand/';
		$upload->autoSub = true;//使用子目录保存
		$upload->subType = 'date';//使用日期子目录
		$upload->dateFormat = 'Y-m';//按月份保存
		if ($upload->upload()) {
			$info = $upload->getUploadFileInfo();
			echo json_encode(array('status' => 1, 'path' => $info[0]['savename']));
		} else {
			echo json_encode(array('status' => 0, 'msg' => $upload->getErrorMsg()));
		}
	}

	/**
	 * 上传列表页图片
	 */
	Public function uploadList () {
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize = 2000000 ;	//最大上传大小
		$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');//充许上传文件的类型
		$upload->saveRule = 'uniqid';
		$upload->savePath = './Uploads/List/';
		$upload->autoSub = true;//使用子目录保存
		$upload->subType = 'date';//使用日期子目录
		$upload->dateFormat = 'Y-m';//按月份保存
		$upload->thumb = true;	//生成缩略图
		$upload->thumbMaxWidth = 200; //缩略图宽度
		$upload->thumbMaxHeight = 130;	//缩略图高度
		$upload->thumbPrefix = '';	//缩略图前缀名
		$upload->uploadReplace = true;	//覆盖原图
		$upload->thumbRemoveOrigin = false;
		$upload->thumbPath = './Uploads/List/' . date('Y-m') . '/';
		if ($upload->upload()) {
			$info = $upload->getUploadFileInfo();
			echo json_encode(array('status' => 1, 'path' => $info[0]['savename']));
		} else {
			echo json_encode(array('status' => 0, 'msg' => $upload->getErrorMsg()));
		}
	}

	/**
	 * 商品图册上传
	 */
	Public function uploadPic () {
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize = 2000000 ;	//最大上传大小
		$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');//充许上传文件的类型
		$upload->saveRule = 'uniqid';
		$upload->savePath = './Uploads/Picture/';
		$upload->autoSub = true;//使用子目录保存
		$upload->subType = 'date';//使用日期子目录
		$upload->dateFormat = 'Y-m';//按月份保存
		$upload->thumb = true;	//生成缩略图
		$upload->thumbMaxWidth = '300,100'; //缩略图宽度
		$upload->thumbMaxHeight = '200,80';	//缩略图高度
		$upload->thumbPrefix = 'medium_,mini_';	//缩略图前缀名
		$upload->uploadReplace = true;	//覆盖原图
		$upload->thumbRemoveOrigin = false;
		$upload->thumbPath = './Uploads/Picture/' . date('Y-m') . '/';
		if ($upload->upload()) {
			$info = $upload->getUploadFileInfo();
			$pic =  explode('/', $info[0]['savename']);
			$path = array(
				'max' => $info[0]['savename'],
				'medium' => $pic[0] . '/medium_' . $pic[1],
				'mini' => $pic[0] . '/mini_' . $pic[1]
				);
			echo json_encode(array('status' => 1, 'path' => $path));
		} else {
			echo json_encode(array('status' => 0, 'msg' => $upload->getErrorMsg()));
		}
	}

	/**
	 * 商品介绐Ueditor图片上传
	 */
	Public function intro() {
		header("Content-Type: text/html; charset=utf-8");
    	error_reporting(E_ERROR | E_WARNING);
    	$title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
    	import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize = 2000000 ;	//最大上传大小
		$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');//充许上传文件的类型
		$upload->saveRule = 'uniqid';
		$upload->savePath = './Uploads/intro/';
		$upload->autoSub = true;//使用子目录保存
		$upload->subType = 'date';//使用日期子目录
		$upload->dateFormat = 'Y-m';//按月份保存
		$upload->upload();
		$info = $upload->getUploadFileInfo();
		$url = $info[0]['savename'];
		echo json_encode(array(
			'url' => $url,
			'title' => $title,
			'state' => 'SUCCESS'
			));
	}
}
?>
